<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入项目名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['resource:application:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList(false)"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="applicationList" @selection-change="handleSelectionChange" border
              highlight-current-row align="center">
      <el-table-column label="主键ID" align="center" prop="id"/>
      <el-table-column label="项目名称" align="center" prop="name"/>
      <el-table-column label="描述信息" align="center" prop="description"/>
      <el-table-column label="备注信息" align="center" prop="remark"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="160"/>
      <el-table-column label="所属的集群ID" align="center" prop="clusterId"/>
      <el-table-column prop="InUse" label="是否使用中" align="center" min-width="200">
        <template slot-scope="scope">
          <TableState v-if="scope.row.inUse" showTitle="使用中" stateType="positive"/>
          <TableState v-else showTitle="未使用" stateType=""/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['resource:application:edit']"
              v-if="!scope.row.inUse"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="!scope.row.inUse"
              v-hasPermi="['resource:application:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList(false)"
    />

    <!-- 弹出新增框 -->
    <el-dialog :title="applicationTitle" :visible.sync="showApplicationInfo" append-to-body
               :close-on-click-modal="false" :before-close="closeClusterDialog">
      <AddApplicationInfos ref="AddApplicationInfosRef" @applicationSaveOk="applicationSaveOk"
                           :form-data="formData"/>
    </el-dialog>


  </div>
</template>

<script>
import AddApplicationInfos from "@/views/resource/application/AddApplicationInfos";
import TableState from "@/components/TableState";
import {
  listApplication,
  getApplication,
  delApplication
} from "@/api/resource/application";

import {getCurrentClusterInfos} from "@/api/resource/cluster";
import {getFlinkversions} from "@/api/resource/flinkversions";


export default {
  name: "Application",
  components: {
    AddApplicationInfos,
    TableState
  },
  data() {
    return {
      selectedClusterId: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目管理表表格数据
      applicationList: [],
      formData: {
        clusterId: '',
        name: '',
        description: '',
        remark: '',
        currentClusterList: []
      },
      // 弹出层标题
      showApplicationInfo: false,
      applicationTitle: '',

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null
      }
    };
  },
  mounted() {
    const timer = window.setInterval(() => this.getList(true), 2000)
    this.$once('hook:beforeDestroy', () => {
      clearInterval(timer)
    })
  },
  created() {
    this.getList(false);
  },
  methods: {
    /** 查询项目管理表列表 */
    getList(isTimer) {
      if (!isTimer) {
        this.loading = true;
      }
      listApplication(this.queryParams).then(response => {
        this.applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(false);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //获取有权限的集群列表
      this.queryClusterList('add', null);
    },

    initFormData(value) {
      if (value != null) {
        this.formData.id = value.id;
        this.formData.clusterId = value.clusterId;
        this.formData.name = value.name;
        this.formData.description = value.description;
        this.formData.remark = value.remark;
      } else {
        this.formData.id = '';
        this.formData.clusterId = '';
        this.formData.name = '';
        this.formData.description = '';
        this.formData.remark = '';
      }

    },


    //获取集群列表信息
    queryClusterList(editType, currentData) {
      getCurrentClusterInfos().then(res => {
        this.formData.currentClusterList = res.rows;
        if (this.formData.currentClusterList.length > 0) {
          if (editType === 'add') {
            this.applicationTitle = '新建项目'
            this.initFormData(null);
          } else {
            this.applicationTitle = '编辑项目'
            if (currentData != null) {
             this.initFormData(currentData)
            }
          }
          this.showApplicationInfo = true;
        }
      }).catch(err => {
        this.$message.error('获取集群信息失败：' + err)
      })
    },

    closeClusterDialog() {
     this.initFormData(null)
      this.showApplicationInfo = false
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids
      getApplication(id).then(response => {
        //this.form = response.data;
        if (response.data.inUse) {
          this.$message.warning('正在使用的项目不能删除！')
          return;
        }
        this.queryClusterList('update', response.data)
      });
    },

    /**
     * 项目保存成功
     */
    applicationSaveOk() {
      this.getList(false);
      this.showApplicationInfo = false;
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目管理表编号为"' + ids + '"的数据项？').then(function () {
        return delApplication(ids);
      }).then(() => {
        this.getList(false);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('resource/application/export', {
        ...this.queryParams
      }, `application_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
